package com.gw.quotes.activity;

import java.util.ArrayList;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.gw.quotes.R;
import com.gw.quotes.entity.business.GetEstimateEntity;
import com.gw.quotes.entity.business.GetEstimateEntity.Estimatedetail;
import com.gw.quotes.net.BaseServiceInter;
import com.gw.quotes.net.HttpCallBack;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.gw.quotes.net.service.GetEstimateServiceImpl;
import com.gw.quotes.util.CommonTools;
import com.yxw.view.NoScrollListView;
import com.yxw.view.YXWVerticalScrollView;

public class MineEvaluationDetailActivity extends BaseActivity {

	@ViewInject(id = R.id.back_img, click = "back")
	private ImageView back_img;
	@ViewInject(id = R.id.text_suppliername)
	private TextView text_suppliername;
	@ViewInject(id = R.id.text_buyerorgname)
	private TextView text_buyerorgname;
	@ViewInject(id = R.id.text_ordercode)
	private TextView text_ordercode;
	@ViewInject(id = R.id.text_point)
	private TextView text_point;
	@ViewInject(id = R.id.nolistview)
	private NoScrollListView nolistview;
	@ViewInject(id = R.id.verticalScrollView)
	private YXWVerticalScrollView verticalScrollView;

	private String sContractguid = null;
	private String sOrdercode = null;

	private GetEstimateEntity estimateEntity;

	private EvaluationAdapter evaluationAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mine_evaluation_detail);
		sContractguid = getIntent().getStringExtra("Contractguid");
		sOrdercode = getIntent().getStringExtra("Ordercode");
		requestList();
	}

	private void requestList() {
		BaseServiceInter httpService = new GetEstimateServiceImpl();
		Request request = new Request();
		request.setLoginname(getLoginUser().getLoginname());
		request.setTicket(getLoginUser().getTicket());
		request.setContractguid(sContractguid);
		dialog.show();
		Response response = httpService.handler(request,
				new HttpCallBack<GetEstimateEntity>() {
					@Override
					public void onFinished(GetEstimateEntity response) {
						if (response != null
								&& response.getResult() == Response.RESULT_SUCCESS) {
							estimateEntity = response;
							updateUI();
						} else {
							CommonTools.showShortToast(
									MineEvaluationDetailActivity.this,
									"网络请求失败！");
						}
						dialog.dismiss();
					}
				});
	}

	/**
	 * 更新UI
	 */
	public void updateUI() {
		text_buyerorgname.setText(estimateEntity.getBuyerorgname());
		text_suppliername.setText(estimateEntity.getSuppliername());
		text_ordercode.setText(sOrdercode);
		text_point.setText(estimateEntity.getEstimateorgpoint() + "分");
		evaluationAdapter = new EvaluationAdapter(this,
				estimateEntity.getDetaillist());
		nolistview.setAdapter(evaluationAdapter);
		verticalScrollView.scrollTo(0, 0);
		verticalScrollView.smoothScrollTo(0, 0);
	}

	/**
	 * onClick 返回按钮
	 * 
	 * @param v
	 */
	public void back(View v) {
		finish();
	}

	class EvaluationAdapter extends BaseAdapter {

		private Context context;
		private ArrayList<Estimatedetail> list;

		public EvaluationAdapter(Context context, ArrayList<Estimatedetail> list) {
			this.context = context;
			this.list = list;
		}

		@Override
		public int getCount() {
			return list == null ? 0 : list.size();
		}

		@Override
		public Object getItem(int position) {
			return list == null ? null : list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(this.context).inflate(
						R.layout.item_evaluation_detail, null);
			}
			if (list == null) {
				return convertView;
			}
			Estimatedetail eva = list.get(position);
			TextView textView = (TextView) convertView
					.findViewById(R.id.text_itemname);
			RadioButton radioButton = (RadioButton) convertView
					.findViewById(R.id.radio_optionname);
			textView.setText((position+1)+"."+eva.getItemname());
			radioButton.setText(eva.getOptionname());
			return convertView;
		}
	}

}
